package org.mogu;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.mogu.control.model.IMUser;
import org.mogu.service.GetSyncUrl;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.CityData;
import org.mogu.tools.HeaderPic;
import org.mogu.tools.UrlSync;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class QQ_Info extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener{
	Spinner spin_sheng;
	Spinner spin_city;
	CityData cityData=new CityData();
	EditText search_nickname;
	EditText search_desc;
	RadioButton girl;
	RadioButton boy;
	Button save;
	private UrlSync urlSync=new UrlSync();
	public Handler mMainHandler;
	private String maincode=null;
//	private String code=null;
	Intent intent;
	IMUser imUser;
	DatePicker date;
	private ImageView headIcon=null;
	private boolean hasCity=false;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		hasCity=false;
		intent=this.getIntent();  
		Bundle bunde = intent.getExtras();
		imUser = (IMUser) bunde.getSerializable("iMUser");
		if("0".equals(imUser.getTitle())){
			setContentView(R.layout.qq_info_window);
			headIcon = (ImageView)findViewById(R.id.headIcon);
			headIcon.setImageResource(HeaderPic.getHeader(imUser.getHeader()));
		}else{
			setContentView(R.layout.qq_info_window_read);
		}
		
		spin_sheng=(Spinner)findViewById(R.id.spinner_sheng);
		spin_sheng.setOnItemSelectedListener(this);
		spin_city=(Spinner)findViewById(R.id.spinner_city);
		search_nickname=(EditText)findViewById(R.id.search_nickname);
		search_desc=(EditText)findViewById(R.id.search_desc);
		girl=(RadioButton)findViewById(R.id.girl);
		boy=(RadioButton)findViewById(R.id.boy);
		save=(Button)findViewById(R.id.qq_info_save);
		save.setOnClickListener(this);
		date=(DatePicker)findViewById(R.id.birthday);
		
		maincode = bunde.getString("maincode");
//		code = bunde.getString("code");
		search_nickname.setText(imUser.getNicename());
		search_desc.setText(imUser.getDesc());
		if("0".equals(imUser.getTitle())){
			save.setVisibility(View.VISIBLE);
		}else{
			save.setVisibility(View.GONE);
		}
		
		ArrayAdapter<String> sheng=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityData.sheng_search);
		sheng.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_sheng.setAdapter(sheng);
		
		if(imUser.getSheng()!=null&&!"".equals(imUser.getSheng())){
			spin_sheng.setSelection(Integer.valueOf(imUser.getSheng()));
			
		}
		
		if(imUser.getSex()!=null&&!"".equals(imUser.getSex())){
			if("true".equals(imUser.getSex())){
				girl.setChecked(true);
			}
			if("false".equals(imUser.getSex())){
				boy.setChecked(true);
			}
		}
		
		if(imUser.getBirthday()!=null&&!"".equals(imUser.getBirthday())){
			date.init(Integer.valueOf(imUser.getBirthday().split("-")[0]), Integer.valueOf(imUser.getBirthday().split("-")[1])-1, Integer.valueOf(imUser.getBirthday().split("-")[2]), null);
		}
		
		mMainHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {

				// // 接收子线程的消息
				if(msg.arg1==1){
//					UrlSync urlSync=(UrlSync)msg.obj;
//					imUser.setUnicename(search_nickname.getText().toString());
//					imUser.setDesc(search_desc.getText().toString());
//					imUser.setSex(String.valueOf(girl.isChecked()));
					onBack(null);
				}
				
			}

		};
	}
	public void changeHeader(View view){
		Intent intent = new Intent();
		intent.setClass(this, HeaderGroup.class);
//		intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
		startActivityForResult(intent, 0);
	}
	
	/*
     * 
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_OK:
			/* 取得来自Activity2的数据，并显示于画面上 */
			Bundle bunde = data.getExtras();
			String str = bunde.getString("data");
			// double height = bunde.getDouble("height");
			imUser.setHeader(Integer.valueOf(str));
			headIcon.setImageResource(HeaderPic.getHeader(imUser.getHeader()));
			break;
		default:
			break;
		}
		
    }
	
	/*
     * 控件单击事件监听器接口函数
     * */
    @Override
    public void onClick(View view){
    	if("".equals(search_nickname.getText().toString())){
    		Toast.makeText(this, "昵称不能为空。", 4000).show();
    	}
    	List<NameValuePair> param=new ArrayList<NameValuePair>();
    		param.add(new BasicNameValuePair("nickname",search_nickname.getText().toString()));
    		imUser.setUnicename(search_nickname.getText().toString());
    		param.add(new BasicNameValuePair("desc",search_desc.getText().toString()));
    		imUser.setDesc(search_desc.getText().toString());
    		if(spin_sheng.getSelectedItemPosition()!=0){
    			imUser.setSheng(String.valueOf(spin_sheng.getSelectedItemPosition()));
    			param.add(new BasicNameValuePair("sheng",String.valueOf(spin_sheng.getSelectedItemPosition())));
    		}
    		if(spin_city.getSelectedItemPosition()!=0){
    			imUser.setCity(String.valueOf((spin_sheng.getSelectedItemPosition())*10).substring(0, 2)+String.valueOf(spin_city.getSelectedItemPosition()));
    			param.add(new BasicNameValuePair("city",String.valueOf((spin_sheng.getSelectedItemPosition())*10).substring(0, 2)+String.valueOf(spin_city.getSelectedItemPosition())));
    		}
    		imUser.setSex(String.valueOf(girl.isChecked()));
			param.add(new BasicNameValuePair("sex",String.valueOf(girl.isChecked())));
			imUser.setBirthday(""+date.getYear()+"-"+(date.getMonth()+1)+"-"+date.getDayOfMonth());
			param.add(new BasicNameValuePair("birthday",""+date.getYear()+"-"+(date.getMonth()+1)+"-"+date.getDayOfMonth()));
			param.add(new BasicNameValuePair("header",""+imUser.getHeader()));
    	urlSync.setPrarm(param);
    	urlSync.setResultType("35");
		urlSync.setModth(UrlSync.POST);
		urlSync.setNotice(false);
		urlSync.setToast(true);
		urlSync.setUri(GetSyncUrl.getAppUrlByCode(maincode)+"/UpdateInfo");
		urlSync.setMaincode(maincode);
		urlSync.setSyncType(UrlSync.INFOSEND);
		
		urlSync.setHandler(mMainHandler);
//		urlSync.setCode(imUser.getCode());
		urlSync.setToastContentSu("修改用户信息成功了。");
		urlSync.setToastContentFa("修改用户信息失败了。");
		
		
		urlSync.setImUser(imUser);
		synchronized (Convert.class) {
			Convert.syncList.add(0,urlSync);
		}
		Toast.makeText(this, "正在提交。", 4000).show();
    }
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
		ArrayAdapter<String> city=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,cityData.city_search.get(cityData.sheng_search[arg2]));
		city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin_city.setAdapter(city);
		if(!hasCity){
			if(imUser.getCity()!=null&&!"".equals(imUser.getCity())&&imUser.getCity().length()>2){
				spin_city.setSelection(Integer.valueOf(imUser.getCity().substring(2)));
			}
			hasCity=true;
		}
		// TODO Auto-generated method stub
//		changeAdapter(groupidlist.get(arg2));
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	public void onBack(View view) {
    	if(intent==null){
    		return;
    	}
    	
    	this.setResult(RESULT_FIRST_USER, intent);
    	this.finish(); 
    	
    }
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
//				) { // 按下的如果是BACK，同时没有重复
//
//			onBack();
//			return true;
//		}
//		return super.onKeyDown(keyCode, event);
//	}
}
